package com.chainyoung.common.appcommon.hdwallet;

import android.text.TextUtils;
import android.util.Pair;

import com.chainyoung.common.appcommon.entity.HdWalletCache;
import com.chainyoung.common.utils.RxRealmUtils;

import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.FlowableEmitter;
import io.reactivex.functions.Consumer;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmResults;

/**
 * 获取以及添加更新本地钱包
 */
public class HdWalletDAO {

    private RealmConfiguration realmConfiguration;

    public HdWalletDAO(RealmConfiguration realmConfiguration) {
        this.realmConfiguration = realmConfiguration;
    }


    public Flowable<HdWalletCache> addHdWalletCache(final HdWalletCache item) {
        return RxRealmUtils
                .flowableExec(realmConfiguration, new Consumer<Pair<FlowableEmitter, Realm>>() {
                    @Override
                    public void accept(final Pair<FlowableEmitter, Realm> pair) throws Exception {
                        pair.second.executeTransaction(new Realm.Transaction() {
                            @Override
                            public void execute(Realm r) {
                                pair.first.onNext(r.copyFromRealm(r.copyToRealmOrUpdate(item)));
                                pair.first.onComplete();
                            }
                        });
                    }
                });
    }


    public Flowable<List<HdWalletCache>> getHdWalletCaches(String coinType) {
        try (final Realm realm = Realm.getInstance(realmConfiguration)) {

            RealmResults<HdWalletCache> queryResult = null;
            if (TextUtils.isEmpty(coinType)) {
                queryResult = realm.where(HdWalletCache.class).findAll();
            } else {
                queryResult = realm.where(HdWalletCache.class).equalTo("coinType", coinType).findAll();
            }
            if (queryResult != null) {
                return Flowable.just(realm.copyFromRealm(queryResult));
            } else {
                return Flowable.empty();
            }
        }
    }

   /* public Flowable<Boolean> deleteHdWalletCacheByUrl(final String time, final String url) {
        return RxRealmUtils
                .flowableExec(realmConfiguration, new Consumer<Pair<FlowableEmitter, Realm>>() {
                    @Override
                    public void accept(final Pair<FlowableEmitter, Realm> pair) throws Exception {
                        pair.second.executeTransactionAsync(new Realm.Transaction() {
                            @Override
                            public void execute(Realm r) {
                                final boolean isSuccess = r.where(HdWalletCache.class)
                                        .equalTo("updtime", time)
                                        .equalTo("url", url)
                                        .findAll()
                                        .deleteAllFromRealm();
                                pair.first.onNext(isSuccess);
                                pair.first.onComplete();
                            }
                        });
                    }
                });
    }*/


   /* public Flowable<Boolean> deleteScheduleCacheAll() {
        return RxRealmUtils
                .flowableExec(realmConfiguration, new Consumer<Pair<FlowableEmitter, Realm>>() {
                    @Override
                    public void accept(final Pair<FlowableEmitter, Realm> pair) throws Exception {
                        pair.second.executeTransactionAsync(new Realm.Transaction() {
                            @Override
                            public void execute(Realm r) {
                                final boolean isSuccess = r.where(HdWalletCache.class)
                                        .findAll()
                                        .deleteAllFromRealm();
                                pair.first.onNext(isSuccess);
                                pair.first.onComplete();
                            }
                        });
                    }
                });
    }*/


}
